// pages/setphones/setphones.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:"获取验证码",
    countdown:60,
    add:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  this.setData({
		  session_str: options.str
	  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
	  
  },
  phone:function(e){
   this.setData({
     phone: e.detail.value
   })
  },

  code:function(){
    var countdown = this.data.countdown;
    var that =this;
    var add = this.data.add;
    var phone = this.data.phone;
     if(!phone){
       wx.showToast({
         title: "请输入手机号",
         icon: 'none',
         duration: 2000
       })
       return false
     } 
    if (!isPoneAvailable(phone)) {
      wx.showToast({
        title: "请正确填写手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    } 
    //验证手机号
    function isPoneAvailable(zzk) {
      var myreg = /^[1][1,2,3,4,5,6,7,8,9,0][0-9]{9}$/;
      if (!myreg.test(zzk)) {
        return false;
      } else {
        return true;
      }
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      method: "POST",
		url: url + 'Common/sendCode',
      data: {
        telephone: phone,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          wx.showToast({
            title: "发送成功",
            icon: 'success',
            duration: 2000
          })
          settime();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })



    function settime() {
      if (countdown == 0) {
        countdown = 60;
        that.setData({
          add: 1,
          countdown: countdown
        })
        return false;
      } else {
        countdown--;
        that.setData({
          add: 2,
          countdown: countdown
        })
      }
      setTimeout(function () {
        settime();
      }, 1000);
    }
  
  },

  codes:function(e){
    this.setData({
      code2: e.detail.value
    })
  },


  zzkbwls: function () {
	  var session_str = this.data.session_str;//获取前一页传过来的密钥
    var phone = this.data.phone;
    var vcode = this.data.code2;
	console.log(vcode)
    if (!phone) {
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!isPoneAvailable(phone)) {
      wx.showToast({
        title: "请正确输入手机号",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    function isPoneAvailable(zzk) {
      var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!myreg.test(zzk)) {
        return false;
      } else {
        return true;
      }
    }
	  if (!vcode) {
      wx.showToast({
        title: "请输入验证码",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    
    wx.showLoading({
      title: '加载中',
    })
	  wx.getUserInfo({
		  success: function (res) {
			  var userinfo = res.userInfo;
			  var gender = userinfo.gender; //性别  1男  0：女
			  var avatarUrl = userinfo.avatarUrl; //头像  
			  var nickName = userinfo.nickName; //昵称
			  // 进行授权操作
			  wx.getSetting({
				  success: function (res) {
					  if (res.authSetting['scope.userInfo']) {
						  // var session_str = wx.getStorageSync('session_str');
						  // wx.removeStorageSync('session_str');

						  wx.request({
							  url: url + 'User/setUserInfoByPhone',
							  data: {
								  session_str: session_str,
								  nickName: nickName,
								  avatarUrl: avatarUrl,
								  gender: gender,
								  vcode: vcode,
								  telephone: phone
							  },
							  header: {},
							  method: 'post',
							  dataType: 'json',
							  responseType: 'text',
							  success: function (res) {
								  wx.hideLoading();
								  if (res.data.code == 200) {
									  wx.setStorageSync('session_str', session_str); //判断是否登陆的凭证
									  wx.navigateBack({
										  delta: 2,
									  })
								  }else{
									  wx.showToast({
										  title: res.data.msg,
										  icon: 'none',
										  duration: 2000
									  })
								  }
							  },
							  fail: function (res) {

								  wx.hideLoading();
							  }
						  });
					  } else {

					  }
				  },
				  fail: function (res) { },
				  complete: function (res) { },
			  })

		  },
		  fail: function () { }
	  })												
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
})